'use strict';

/**
 * @ngdoc function
 * @name afasApp.controller:BankCtrl
 * @description
 * # BankCtrl
 * Controller of the afasApp
 */
angular.module('afasApp')
  .controller('ClientsCtrl', ['$scope', '$state', 'Services', function ($scope, $state, Services) {

    // $scope.clients = [];
    $scope.clients = [];

    $scope.params = {
      keyword: $scope.searchText
    };

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('clients', data)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.clients = results.data;
      });
    }
    getResultsPage(1);

    $scope.delete = function (item) {
      Services.delete('clients', item);
    };
  }])
  .controller('ClientCreateCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', '_', function($scope, Services, $state, $stateParams, $filter, _){

    // VARIABLE
    $scope.newItem = {
      'Contacts': [],
      'ContactIds': []
    };

    // CONTACTS
    $scope.searchContacts = function (val) {
      $scope.contactResults = [];
      var params = {
        'student_name': val,
        'pageSize'    : 10
      };
      return Services.getItems('contacts', params)
      .then(function(results){
        return results.data;
      });
    };

    $scope.selectContact = function (contact) {
      $scope.newItem.Contacts.push(contact);
      $scope.newItem.ContactIds.push(contact.Id);
    };

    $scope.removeContact = function (ct) {
      console.log(ct);
      $scope.newItem.ContactIds = _.without($scope.newItem.ContactIds, ct.Id);
      $scope.newItem.Contacts = _.without($scope.newItem.Contacts, ct);
    };

    $scope.valid = function (contact) {
      // console.log(contact);
      // console.log(_.contains($scope.newItem.Contacts, contact));
      return !(_.contains($scope.newItem.ContactIds, contact.Id));
    };

    $scope.onSelect = function ($item, $model, $label) {
      if (!_.contains($scope.newItem.ContactIds, $item.Id)) {
        $scope.newItem.Contacts.push($item);
        $scope.newItem.ContactIds.push($item.Id);
      }
    };

    // [POST] /CLIENTS
    $scope.add = function () {
      // console.log($scope.newItem);
      Services.add('clients', $scope.newItem)
      .then(function(results){
        // console.log(results);
        $scope.newItem = {};
        $state.go( 'clients.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

  }])
  .controller('ClientCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.current = {};
    $scope.currentID = $stateParams.id;
    $scope.current = Services.getItem('clients', $stateParams.id);

    $scope.update = function () {
      Services.update('clients', $scope.current, $scope.currentID);
      $state.go( 'clients.show', {id: $scope.currentID} );
    };
  }]);